package com.wyz.socket.core.base.frame;

import com.wyz.socket.core.base.IoArgs;
import com.wyz.socket.core.base.frame.receive.*;

public class ReceiveFrameFactory {
    public static AbstractReceiveFrame createInstance(IoArgs args) {
        byte[] buffer = new byte[Frame.FRAME_HEADER_LENGTH];
        args.writeTo(buffer, 0, buffer.length);
        byte type = buffer[2];
        switch (type) {
            case Frame.TYPE_HEADER:
                return new ReceiveHeaderFrame(buffer);
            case Frame.TYPE_ENTITY:
                return new ReceiveEntityFrame(buffer);
            case Frame.TYPE_SEND_CANCEL:
                return new CancelReceiveFrame(buffer);
            case Frame.TYPE_HEARTBEAT:
                return HeartbeatReceiveFrame.INSTANCE;
            default:
                throw new UnsupportedOperationException("unsupported frame type >" + type);
        }
    }
}
